//
//  BKXXSQCheckStudentsView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/25.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BKXXSQCheckStudentsView.h"
#import "SYRequestAPI+FuDao.h"


@implementation BKXXSQCheckStudentsView
//- (instancetype)initWithFrame:(CGRect)frame{
//    if ([super initWithFrame:frame]) {
//        [self initSubView];
//        [self layout];
//    }
//    return self;
//}
- (instancetype)initWithFrame:(CGRect)frame andFuDaoID:(NSString *)fdID{
    if ([super initWithFrame:frame]) {
        self.fdID = fdID;
        [self initSubView];
        [self layout];
    }
    return self;
}
- (void)initSubView{
    pageIndex = 1;
    pageSize = 30;
    tutorLessonId = self.fdID;
    keyWord = @"";
    _xueShengArray = [NSMutableArray new];
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.navView];
    [self addSubview:self.searchView];
    [self addSubview:self.tableList];
    [self getChaKanXueShengReuqest];
    
    [_tableList headerRefreshBlock:^{
        self->pageIndex = 1;
        [self->_xueShengArray removeAllObjects];
        [self getChaKanXueShengReuqest];
    }];
    [_tableList fooderRefreshBlock:^{
        [self getChaKanXueShengReuqest];
    }];
}
- (void)layout{
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_navView.mas_bottom);
        make.height.equalTo(@100);
    }];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0).offset(0);
        make.top.equalTo(self->_searchView.mas_bottom);
        make.bottom.equalTo(@0);
    }];
}
- (BKXianShangSearchView *)searchView{
    if (!_searchView) {
        BKXianShangSearchView *view = [[BKXianShangSearchView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 100)];
        view.backgroundColor = [UIColor whiteColor];
        //添加监听
        [view.searchView.searchTF addTarget:self action:@selector(textFieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
       
        view.searchView.block = ^(NSString * _Nonnull keyStr) {
            self->keyWord = keyStr;
            [self->_xueShengArray removeAllObjects];
            [self getChaKanXueShengReuqest];
        };
        
        _searchView = view;
    }
    return _searchView;
}
- (ZXFDCCVNav *)navView{
    if (!_navView) {
        ZXFDCCVNav *view = [[ZXFDCCVNav alloc]init];
        view.backgroundColor = MAINBACKCOLOR;
        view.delegate = self;
        view.titleLabe.text = @"查看学生";
        _navView = view;
    }
    return _navView;
}
//监听改变方法
- (void)textFieldTextDidChange:(UITextField *)textChange{
  //  NSLog(@"文字改变：%@",textChange.text);
    keyWord = textChange.text;
}
- (void)backClick{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.size.width,0, self.frame.size.width, self.frame.size.height);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[BKXXSQCheckStudentsViewCell class] forCellReuseIdentifier:@"BKXXSQCheckStudentsViewCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _xueShengArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BKXXSQCheckStudentsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BKXXSQCheckStudentsViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[BKXXSQCheckStudentsViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BKXXSQCheckStudentsViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    ChaKanXueShengModel *model = _xueShengArray[indexPath.row];
    cell.dataArray = @[model.serial,model.name,model.lesson,model.mobile,model.statusName,model.createDate];
    [cell reloadViewLayout];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[BKXXSQCSHeader alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"教材",@"课时",@"类型",@"时间"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    _titleView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
    [_titleView relodView];
    return _titleView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    mainView.frame = CGRectMake(0, 0, self.frame.size.width, 30);
    return mainView;
}

#pragma mark - 查看学生
- (void)getChaKanXueShengReuqest{
    NSDictionary *parama = @{@"tutorLessonId":tutorLessonId,
                             @"keyWord":keyWord,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI fd_TutorTutorClassServiceTutorLessonOffLineStudents:parama success:^(id responseObject) {
      //  NSLog(@"辅导-补课管理-查看学生 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        if (array.count>0) {
            self->pageIndex++;
        }
        for (NSDictionary *dic in array) {
            ChaKanXueShengModel *model = [ChaKanXueShengModel yy_modelWithJSON:dic];
            [self->_xueShengArray addObject:model];
        }
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    }];
}
@end
